package com.small.xx.ai.apidoc.controller;

import com.small.xx.ai.apidoc.dto.AppServiceRequest;
import com.small.xx.ai.apidoc.dto.AppServiceResponse;
import com.small.xx.ai.apidoc.service.AppServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用服务管理REST控制器
 */
@RestController
@RequestMapping("/api/v1/app-services")
public class AppServiceRestController {
    
    @Autowired
    private AppServiceService appServiceService;
    
    /**
     * 创建应用服务
     * 
     * @param request 应用服务请求
     * @return 应用服务响应
     */
    @PostMapping
    public ResponseEntity<AppServiceResponse> createAppService(@RequestBody AppServiceRequest request) {
        try {
            AppServiceResponse response = appServiceService.createAppService(request);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }
    
    /**
     * 根据ID获取应用服务
     * 
     * @param id 应用服务ID
     * @return 应用服务响应
     */
    @GetMapping("/{id}")
    public ResponseEntity<AppServiceResponse> getAppServiceById(@PathVariable Long id) {
        AppServiceResponse response = appServiceService.getAppServiceById(id);
        if (response == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(response);
    }
    
    /**
     * 分页获取应用服务列表
     * 
     * @param page 页码
     * @param size 每页大小
     * @return 应用服务响应列表
     */
    @GetMapping
    public ResponseEntity<List<AppServiceResponse>> getAppServicesByPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        List<AppServiceResponse> appServices = appServiceService.getAppServicesByPage(page, size);
        return ResponseEntity.ok(appServices);
    }
    
    /**
     * 获取所有应用服务
     * 
     * @return 应用服务响应列表
     */
    @GetMapping("/all")
    public ResponseEntity<List<AppServiceResponse>> getAllAppServices() {
        List<AppServiceResponse> appServices = appServiceService.getAllAppServices();
        return ResponseEntity.ok(appServices);
    }
    
    /**
     * 更新应用服务
     * 
     * @param id 应用服务ID
     * @param request 应用服务请求
     * @return 应用服务响应
     */
    @PutMapping("/{id}")
    public ResponseEntity<AppServiceResponse> updateAppService(@PathVariable Long id, @RequestBody AppServiceRequest request) {
        AppServiceResponse response = appServiceService.updateAppService(id, request);
        if (response == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(response);
    }
    
    /**
     * 删除应用服务
     * 
     * @param id 应用服务ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAppService(@PathVariable Long id) {
        boolean deleted = appServiceService.deleteAppService(id);
        if (!deleted) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }
}